package com.lanou.bookstore.order.dao;

import com.lanou.bookstore.book.dao.BookDao;
import com.lanou.bookstore.book.domain.Book;
import com.lanou.bookstore.order.domain.Order;
import com.lanou.bookstore.order.domain.OrderItem;
import com.lanou.bookstore.util.JDBCUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class OrderDao {

    //添加订单
    public void addOrder(Order order) {
        Connection conn = JDBCUtil.getConn();
        try {
            String sql = "insert into orders values (?, ?, ?, ?, ?, ?)";
            PreparedStatement statement
                    = conn.prepareStatement(sql);
            statement.setString(1, order.getOid());
            statement.setString(2, order.getOrderTime());
            statement.setDouble(3, order.getTotal());
            statement.setInt(4, order.getState());
            statement.setString(5, order.getUid());
            statement.setString(6, order.getAddress());
            statement.executeUpdate();
            statement.close();
        } catch (Exception e) {
            try {
                conn.rollback();
            } catch (SQLException e1) {
                throw new RuntimeException(e);
            }
        }
    }

    //添加订单中的书籍
    public void addOrderItemList(List<OrderItem> orderItemList) {
        Connection conn = JDBCUtil.getConn();
        try {
            for (OrderItem orderItem : orderItemList) {
                String sql = "insert into orderitem values (?, ?, ?, ?, ?)";
                PreparedStatement statement
                        = conn.prepareStatement(sql);
                statement.setString(1, orderItem.getIid());
                statement.setInt(2, orderItem.getCount());
                statement.setDouble(3, orderItem.getSubtotal());
                statement.setString(4, orderItem.getOid());
                statement.setString(5, orderItem.getBid());
                statement.executeUpdate();
                statement.close();
            }
        } catch (Exception e) {
            try {
                conn.rollback();
            } catch (SQLException e1) {
                throw new RuntimeException(e);
            }
        }
    }

    //查询这个订单中的所有书
    private List<OrderItem> findByOid(String oid) {
        BookDao bookDao = new BookDao();
        List<OrderItem> list = new ArrayList<>();
        Connection conn = JDBCUtil.getConn();
        String sql = "select * from orderitem where oid = ?";
        PreparedStatement statement = null;
        try {
            statement = conn.prepareStatement(sql);
            statement.setString(1, oid);
            ResultSet rs = statement.executeQuery();
            OrderItem orderItem = null;
            while (rs.next()) {
                orderItem = new OrderItem();
                orderItem.setIid(rs.getString("iid"))
                        .setCount(rs.getInt("count"))
                        .setSubtotal(rs.getDouble("subtotal"))
                        .setOid(rs.getString("oid"))
                        .setBid(rs.getString("bid"));
                Book book = bookDao.load(orderItem.getBid());
                orderItem.setBook(book);
                list.add(orderItem);
            }
            rs.close();
            statement.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    //查询这个人的所有订单
    public List<Order> findByUid(String uid) {
        List<Order> orders = new ArrayList<>();
        Connection conn = JDBCUtil.getConn();
        String sql = "select * from orders where uid = ?";
        PreparedStatement statement
                = null;
        try {
            statement = conn.prepareStatement(sql);
            statement.setString(1, uid);
            ResultSet rs = statement.executeQuery();
            while (rs.next()) {
                //调用 下面的方法
                List<OrderItem> list = findByOid(rs.getString("oid"));
                Order order = new Order();
                order.setOid(rs.getString("oid"))
                        .setOrderTime(rs.getString("ordertime"))
                        .setTotal(rs.getDouble("total"))
                        .setState(rs.getInt("state"))
                        .setUid(rs.getString("uid"))
                        .setAddress(rs.getString("address"))
                        .setOrderItemList(list);
                orders.add(order);
            }
            rs.close();
            statement.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return orders;
    }

    public Order load(String oid) {
        Order order = new Order();
        List<OrderItem> orderItemList = findByOid(oid);
        Connection conn = JDBCUtil.getConn();
        String sql = "select * from orders where oid = ?";
        PreparedStatement statement
                = null;
        try {
            statement = conn.prepareStatement(sql);
            statement.setString(1, oid);
            ResultSet rs = statement.executeQuery();
            rs.next();
            order.setOid(rs.getString("oid"))
                    .setOrderTime(rs.getString("ordertime"))
                    .setTotal(rs.getDouble("total"))
                    .setState(rs.getInt("state"))
                    .setUid(rs.getString("uid"))
                    //地址先不拿
                    .setOrderItemList(orderItemList);
            rs.close();
            statement.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return order;
    }
}
